package org.ocean.online.utils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;

import org.apache.commons.lang.StringUtils;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

public class ZipCompressor {
	static final int BUFFER = 8192;  
	
	boolean includeLib = true;
	
    public ZipCompressor(boolean includeLib) {  
    	this.includeLib = includeLib;
    }  
  
    private static void compress(File file, ZipOutputStream out, String basedir) {  
        /* 判断是目录还是文件 */  
        if (file.isDirectory()) {  
            System.out.println("压缩：" + basedir + file.getName());  
            compressDirectory(file, out, basedir);  
        } else {  
            System.out.println("压缩：" + basedir + file.getName());  
            compressFile(file, out, basedir);  
        }  
    }  
  
    public static void compressFiles(File[] files,OutputStream os,String basedir){
    	
    	CheckedOutputStream cos = new CheckedOutputStream(os,new CRC32());  
        ZipOutputStream out = new ZipOutputStream(cos);  
    	for (int i = 0; i < files.length; i++) {  
            /* 递归 */  
    		if(StringUtils.isBlank(basedir)){
    			compress(files[i], out, "");
    		}else{
    			compress(files[i], out, basedir+ File.separator);
    		}
              
        }
    	try {
			out.close();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
    }
    /** 压缩一个目录 */  
    private static void compressDirectory(File dir, ZipOutputStream out, String basedir) {  
        if (!dir.exists())  
            return;  
  
        File[] files = dir.listFiles();  
        for (int i = 0; i < files.length; i++) {  
            /* 递归 */  
            compress(files[i], out, basedir + dir.getName() + "/");  
        }  
    }  
  
    /** 压缩一个文件 */  
    private static void compressFile(File file, ZipOutputStream out, String basedir) {  
        if (!file.exists()) {  
            return;  
        }  
        try {  
            BufferedInputStream bis =  preProcess(file);
            if(bis==null){
            	return;
            }
            ZipEntry entry = new ZipEntry(basedir + file.getName());  
            out.putNextEntry(entry);  
            int count;  
            byte data[] = new byte[BUFFER];  
            while ((count = bis.read(data, 0, BUFFER)) != -1) {  
                out.write(data, 0, count);  
            }  
            bis.close();  
        } catch (Exception e) {  
            throw new RuntimeException(e);  
        }  
    }
    
    private static BufferedInputStream preProcess(File file) throws FileNotFoundException{
    	if("mutiple.properties".equals(file.getName())){
    		return null;
    	}
    	return new BufferedInputStream(new FileInputStream(file)); 
    }
}
